"use strict";

/* global $ */

// 颜色选择初始说明
// 每一个颜色input都有一个inori-data,内容为对应元素的id或者对应元素的父元素id（迭代用）

$(document).ready(function() {
  $("#color1").colorPicker({
    onColorChange: function onColorChange(id, newValue) {
      var targetID = $(this)
        .prev("input")
        .attr("inori-data");
      var targetEle = $("#" + targetID + " input");
      var _iteratorNormalCompletion = true;
      var _didIteratorError = false;
      var _iteratorError = undefined;

      try {
        for (
          var _iterator = targetEle[Symbol.iterator](), _step;
          !(_iteratorNormalCompletion = (_step = _iterator.next()).done);
          _iteratorNormalCompletion = true
        ) {
          var ele = _step.value;

          $(ele).val(newValue);
        }
      } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
      } finally {
        try {
          if (!_iteratorNormalCompletion && _iterator.return) {
            _iterator.return();
          }
        } finally {
          if (_didIteratorError) {
            throw _iteratorError;
          }
        }
      }

      console.log("ID: " + id + " has been changed to " + newValue);
    }
  });
  $("#color1").change();

  // 初始化其他color
  // 其余颜色选择器集合
  var otherColorPickers = $(".color_picker_others");
  // 循环集合
  var _iteratorNormalCompletion2 = true;
  var _didIteratorError2 = false;
  var _iteratorError2 = undefined;

  try {
    for (
      var _iterator2 = otherColorPickers[Symbol.iterator](), _step2;
      !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done);
      _iteratorNormalCompletion2 = true
    ) {
      var e = _step2.value;

      var id = $(e).attr("inori-data");

      if (
        id === "danwei_color" ||
        id === "peiou_color" ||
        id === "zhixi_color" ||
        id === "qingsu_color" ||
        id === "tongshi_color" ||
        id === "pengyou_color"
      ) {
        // 自定后缀的
        $(e).colorPicker({
          onColorChange: function onColorChange(id, newValue) {
            // 本体
            var targetID = $(this)
              .prev("input")
              .attr("inori-data");
            var targetEle = $("#" + targetID);
            $(targetEle).val(newValue);
            // 自定义
            var targetAltID = targetID.replace(
              /(\w+)(_color)/,
              "$1_zidingyi$2"
            );
            var targetAltEle = $("#" + targetAltID);
            $(targetAltEle).val(newValue);

            console.log("ID: " + id + " has been changed to " + newValue);
          }
        });
        $(e).change();
        // end 自定后缀的
      } else if (id === "jinjian_nianling_min_color") {
        // 特殊对应的
        $(e).colorPicker({
          onColorChange: function onColorChange(id, newValue) {
            // 本体
            var targetID = $(this)
              .prev("input")
              .attr("inori-data");
            var targetEle = $("#" + targetID);
            $(targetEle).val(newValue);
            // 自定义
            var targetAltID = "jinjian_nianling_max_color";
            var targetAltEle = $("#" + targetAltID);
            $(targetAltEle).val(newValue);

            console.log("ID: " + id + " has been changed to " + newValue);
          }
        });
        $(e).change();
        // end 特殊对应的
      } else if (id === "kaohe_color") {
        // 考核需要一起搞到考察里边
        $(e).colorPicker({
          onColorChange: function onColorChange(id, newValue) {
            // kaohe
            var targetID = $(this)
              .prev("input")
              .attr("inori-data");
            var targetEle = $("#" + targetID);
            $(targetEle).val(newValue);
            // kaocha
            var kaochaEle = $("#kaocha_color");
            $(kaochaEle).val(newValue);

            console.log("ID: " + id + " has been changed to " + newValue);
          }
        });
        $(e).change();
        // end 考核需要一起搞到考察里边
      } else {
        // 只有一个对应
        $(e).colorPicker({
          onColorChange: function onColorChange(id, newValue) {
            var targetID = $(this)
              .prev("input")
              .attr("inori-data");
            var targetEle = $("#" + targetID);
            $(targetEle).val(newValue);
            console.log("ID: " + id + " has been changed to " + newValue);
          }
        });
        $(e).change();
        // end 只有一个对应
      }
    }
  } catch (err) {
    _didIteratorError2 = true;
    _iteratorError2 = err;
  } finally {
    try {
      if (!_iteratorNormalCompletion2 && _iterator2.return) {
        _iterator2.return();
      }
    } finally {
      if (_didIteratorError2) {
        throw _iteratorError2;
      }
    }
  }
});
